<template>
  <div class="detail">
    <header class="header">
      <van-icon name="arrow-left" @click="$emit('changeShowState')" />
    </header>
    <div class="title">验证码登录</div>
    <div class="text">为了方便联想，请输入你常用的手机号</div>
    <!-- <div class="btn">手机号登录</div> -->
    <div class="form">
      <van-field v-model="phone" clearable type="tel" label="手机号" />
      <van-field
        v-model="verification"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
      >
        <template #button>
          <van-button
            size="small"
            color="#ffb027"
            @click.stop="sendVerification"
            >{{ verMsg }}</van-button
          >
        </template>
      </van-field>
    </div>
    <div class="text">账号密码登录</div>
    <div class="btn" @click.stop="checkVerification">登录</div>
    <div class="footer">
      <div>其他登录方式</div>
      <div class="ways">
        <div class="img-box">
          <img src="@/../public/images/login/1676031082402.png" alt="" />
        </div>
        <div class="img-box">
          <img src="@/../public/images/login/1676031082403.png" alt="" />
        </div>
        <div class="img-box">
          <img src="@/../public/images/login/1676031082405.png" alt="" />
        </div>
      </div>
      <div class="agreement">
        <input
          type="checkbox"
          v-model="agreement"
        />登录即表示同意<span>《用户协议》</span>和<span>《隐私协议》</span>
      </div>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data: () => {
    return {
      verMsg: "发送验证码",
      agreement: false,
      phone: "",
      verification: "",
      intercept: false,
      timer: null,
    };
  },
  methods: {
    //验证验证码
    checkVerification() {
      let ptt = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
      if (!this.phone || !ptt.test(this.phone) || !this.verification) {
        this.$toast("输入错误");
        return;
      }
      if (!this.agreement) {
        this.$toast("请先同意用户协议");
        return;
      }
      console.log(this.phone, this.verification);
      this.axios({
        method: "post",
        url: `${this.api.baseUrlV3}/apiSign/login`,
        data: qs.stringify({
          username: this.phone,
          captcha: this.verification,
          countryCode: "+86",
        }),
        headers: {
          version: this.version,
          "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
        },
      }).then((res) => {
        // console.log(res);
        // if(res.data.code!=0){
        //   this.$toast("登录信息错误");
        //   return;
        // }else{
        //   this.$router.replace('/home');
        // }
        res;
        localStorage.setItem("token","sauxihuwqbxiubwibiqi");
        let url = localStorage.getItem("lastPage");
        if(!url)url = "/home"
        this.$router.replace(url);
      });
    },
    //发送验证码
    sendVerification() {
      let ptt = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
      if (!this.phone || !ptt.test(this.phone)) {
        this.$toast("输入错误");
        return;
      }
      this.$toast.success("发送成功");
      if (this.intercept) {
        this.$toast.success("验证码已发送");
        return;
      }
      this.intercept = true;
      clearInterval(this.timer);
      let num = 60;
      this.timer = setInterval(() => {
        num--;
        this.verMsg = num + "后重试";
        if (num == 0) {
          this.intercept = false;
          this.verMsg = "发送验证码";
          clearInterval(this.timer);
        }
      }, 1000);
      this.axios({
        method: "post",
        url: `${this.api.baseUrlV3}/apiSign/sendVerifyCode`,
        data: qs.stringify({
          mobile: this.phone,
          countryCode: "+86",
        }),
        headers: {
          version: this.api.version,
          "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
        },
      })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          err;
          this.$toast("服务器繁忙");
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.detail {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  width: 100%;
  min-height: 100vh;
  background-color: var(--bgc-white);
  .header {
    width: 100%;
    height: 0.5rem;
    font-size: 0.2rem;
    line-height: 0.5rem;
    box-sizing: border-box;
    padding: 0 0.1rem;
  }
  .title {
    width: 100%;
    box-sizing: border-box;
    font-size: 0.27rem;
    line-height: 0.27rem;
    color: var(--text-black);
    font-weight: bold;
    padding: 0 0.3rem;
    margin-top: 0.2rem;
    margin-bottom: 0.12rem;
  }
  .text {
    width: 100%;
    box-sizing: border-box;
    padding: 0 0.3rem;
    font-size: 0.13rem;
    line-height: 0.13rem;
    color: var(--text-gray2);
  }
  .form {
    width: 100%;
    box-sizing: border-box;
    padding: 0.2rem;
    padding-bottom: 0.12rem;
  }
  .btn {
    width: 3.2rem;
    height: 0.54rem;
    border-radius: 0.27rem;
    background-color: var(--bgc-yellow);
    font-size: 0.2rem;
    line-height: 0.54rem;
    text-align: center;
    color: var(--text-black);
    margin: 0.5rem auto 0 auto;
  }
  .footer {
    width: 100%;
    box-sizing: border-box;
    padding: 0 0.25rem;
    margin-top: 1.4rem;
    font-size: 0.13rem;
    color: var(--text-gray2);
    .ways {
      display: flex;
      margin: 0.24rem 0.05rem;
      .img-box {
        width: 0.32rem;
        height: 0.32rem;
        border-radius: 50%;
        margin-right: 0.27rem;
      }
    }
    .agreement {
      display: flex;
      align-items: center;
      > input {
        margin-right: 0.1rem;
        margin-top: 0.01rem;
      }
      > span {
        color: var(--text-green);
      }
    }
  }
}
</style>